#include<iostream>
#include<signal.h>
#include<cstring>
#include<unistd.h>
#include<cstdlib>
using namespace std;

void printSigpending(sigset_t& pending)
{
    cout<<"来到打印的地方 pid： "<<getpid()<<" ";

    for(int i =1;i<=31;i++)
    {
        if(sigismember(&pending,i)==1)
            printf("1");
        else
            printf("0");

    }
    printf("\n");

}

void handler(int signo)
{
    cout<<"捕捉 "<<signo<<" 信号成功 pid: "<<getpid()<<endl;
    
    int cnt=20;
    sigset_t pending;
    
    while(cnt--)
    {
        sigemptyset(&pending);
        sigpending(&pending);
        
        printSigpending(pending);
        sleep(1);
    }
}

int main()
{
    struct sigaction newsig,oldsig;
    memset(&newsig,0,sizeof(newsig));//初始化
    memset(&oldsig,0,sizeof(oldsig));//初始化

    sigemptyset(&newsig.sa_mask);//对信号集置空
    sigemptyset(&oldsig.sa_mask);//对信号集置空
    //sigaddset(&newsig.sa_mask,2);//添加信号集
    sigaddset(&newsig.sa_mask,3);//添加信号集
    sigaddset(&newsig.sa_mask,4);//添加信号集
    newsig.sa_handler=handler;

    sigaction(2,&newsig,&oldsig);
    while(1)
    {
        cout<<"pid: "<<getpid()<<endl;
        sleep(1);
    }


    return 0;
}